package com.iking.housekeeping.service.role;

import java.util.ArrayList;
import java.util.List;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.iking.housekeeping.dao.dept.SysDeptMapper;
import com.iking.housekeeping.dao.dtuser.DeptUserMapper;
import com.iking.housekeeping.dao.menu.SysMenuMapper;
import com.iking.housekeeping.dao.role.RoleMapper;
import com.iking.housekeeping.dao.rolemenu.RoleMenuMapper;
import com.iking.housekeeping.entity.menu.SysMenu;
import com.iking.housekeeping.entity.role.Role;
import com.iking.housekeeping.httpbean.role.reqbean.ReturnRole;
import com.iking.housekeeping.util.CodeUtil;

/**
 * 角色管理service
 * 
 * @author 秦甫杰
 *
 */
@Service
public class RoleService {
	
	private static Logger log = Logger.getLogger(RoleService.class);
	
	@Autowired
	private RoleMapper roleMapper;

	@Autowired
	private DeptUserMapper deptUserMapper;

	@Autowired
	private SysDeptMapper sysDeptMapper;

	@Autowired
	private RoleMenuMapper roleMenuMapper;

	@Autowired
	private SysMenuMapper sysMenuMapper;

	/**
	 * 添加角色
	 * 
	 * @param role
	 * @return
	 */
	public int add(Role role) {
		return roleMapper.insertSelective(role);
	}

	/**
	 * 根据名字查询角色
	 * 
	 * @param roleName
	 * @return
	 */
	public Role selectRoleByName(String roleName) {
		return roleMapper.selectRoleByName(roleName);
	}

	/**
	 * 根据ID删除角色
	 * 
	 * @param fId
	 * @return
	 */
	public int delectRoleById(String fId) {
		return roleMapper.deleteByPrimaryKey(fId);
	}

	/**
	 * 根据ID查看用户
	 * 
	 * @param fId
	 * @return
	 */
	public Role selectRolefById(String fId) {
		return roleMapper.selectByPrimaryKey(fId);
	}

	/**
	 * 根据id更新用户
	 * 
	 * @param role
	 * @return
	 */
	public int updateRoleByRole(Role role) {
		return roleMapper.updateByPrimaryKeySelective(role);
	}

	/**
	 * 查看所有的角色
	 * 
	 * @return
	 */
	public List<ReturnRole> selectAllRole() {
		return roleMapper.selectAllRole();
	}
	
	/**
	 * 查看角色返回数级结构
	 */
	public List<ReturnRole> selectAllRoleTree(List<ReturnRole> roleList,String pCode){
		//遍历所有的角色
		List<ReturnRole> returnRoles=new ArrayList<ReturnRole>();
		for (ReturnRole returnRole : roleList) {
			//如果此角色的父级code等于传入的父级code
			if(returnRole.getRolePCode().equals(pCode)) {
				//将此角色加入
				returnRoles.add(returnRole);
				returnRole.setChildren(selectAllRoleTree(roleList,returnRole.getRoleCode()));
			}
		}
		return returnRoles;
	}
	/**
	 * 根据用户ID查询菜单列表
	 * 
	 * @param userId
	 * @return
	 */
	public List<SysMenu> selectRoleByUserId(String userId) {
		/**
		 * 查询语句中添加了DISTINCT去重复，所以不会查询到相同的信息
		 */
		List<SysMenu> menuList = null;
		log.info("userId" + userId);
		List<String> listDept = deptUserMapper.selectDeptByUserId(userId);
		List<String> listString = new ArrayList<String>();
		List<String> listString1 = new ArrayList<String>();
		List<String> listString2 = new ArrayList<String>();
		// 遍历集合获取部门id
		for (String deptUser : listDept) {
			// 根据部门id查询所有的部门信息
			listString.add(deptUser);
			List<String> listDepts = sysDeptMapper.selectSysDeptByDeptId(listString);
			if (listDepts.size() <= 0) {
				log.info("没有任何信息");
			}
			// 遍历所有的部门信息获取角色id
			for (String sysDept : listDepts) {
				// 根据角色id查询菜单的id
				listString1.add(sysDept);
				// 根据所有的角色id查询所有的关联表
				List<String> listRoleMenu = roleMenuMapper.selectRoleMenuByRoleId(listString1);
				for (String roleMenu : listRoleMenu) {
					// 根据菜品id查询所有的菜单
					listString2.add(roleMenu);
					menuList = sysMenuMapper.selectSysMenuByFId(listString2);
				}
			}
		}
		return menuList;
	}

	/**
	 * 根据fID查询角色信息
	 * 
	 * @param fId
	 * @return
	 */
	public Role selectByfId(String fId) {
		return roleMapper.selectByPrimaryKey(fId);
	}

	/**
	 * 添加角色
	 * 
	 * @param role
	 */
	public int addRole(Role role) {
		return roleMapper.insertSelective(role);
	}

	/**
	 * 删除角色
	 * 
	 * @param fId
	 * @return
	 */
	public int deleteRole(String fId) {
		return roleMapper.deleteByPrimaryKey(fId);
	}

	public int updateRole(Role role) {
		return roleMapper.updateByPrimaryKeySelective(role);
	}

	/**
	 * 查询子菜单
	 * 
	 * @param pCode
	 * @return
	 */
	public List<Role> selectRoleByPCode(String pCode, String fId) {
		return roleMapper.selectRoleByPCode(pCode, fId);
	}

	/**
	 * 更改菜单编号
	 * 
	 * @param sysMenu
	 * @param fMenuCode
	 */
	public void changeSysMenuCode(Role role, String fMenuCode) {
		// 1原父级菜单编号
		String oldfMenuCode = role.getRoleCode();
		// 2更改子菜单的父级编号fMenuCode
		role.setRoleCode(fMenuCode);
		// 3更新当前父级菜单的编号
		roleMapper.updateByPrimaryKeySelective(role);
		// 4根据原父级编号获取当下所有子菜单
		List<Role> roleChildren = roleMapper.selectRoleByPCode(oldfMenuCode, null);
		// 5循环更改子菜单编号
		for (Role children : roleChildren) {
			// 5-1更改父级编号中的父级编号
			children.setRolePCode(fMenuCode);
			// 5-2递归更改子菜单编号
			changeSysMenuCode(children, CodeUtil.changeCode(fMenuCode, children.getRoleCode()));
		}
	}

	/**
	 * 菜单排序
	 * 
	 * @param menuList
	 */
	public void soltRoleList(List<Role> roleList) {
		// 1定义一个当前的父级菜单号
		String fMenuCode;
		// 2定义一个新的父级菜单号
		String newfMenuCode;
		// 3遍历所有的父节长度
		for (int i = 0; i < roleList.size(); i++) {
			// 3-1获取当前父级菜单编号
			fMenuCode = roleList.get(i).getRoleCode();
			// 3-2正常菜单编号应为i+1,满足则跳过
			if (i + 1 == CodeUtil.getIndexByCode(fMenuCode)) {
				continue;
			}
			// 3-3如果序号>=10,则倒数第二位补0
			newfMenuCode = CodeUtil.getCodeByPCode(roleList.get(i).getRolePCode(), i);
			// 3-4更改菜单编号
			changeSysMenuCode(roleList.get(i), newfMenuCode);
		}
	}

	/**
	 * 根据角色id查询菜单
	 * 
	 * @param roleIds
	 * @return
	 */
	public List<SysMenu> selectSysMenuByRoleId(List<String> roleIds) {
//		 通过角色Id在角色关联表中查询菜单id
		List<String> roleMenuList = roleMenuMapper.selectRoleMenuByRoleId(roleIds);
		if (roleMenuList.size() <= 0) {
			return new ArrayList<SysMenu>();
		}
		// 根据菜单id查询菜单
		List<SysMenu> listSysMenu = sysMenuMapper.selectSysMenuByFId(roleMenuList);
		return listSysMenu;
	}
}
